/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.gui;

import br.com.editor.contador.*;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import javax.swing.text.BadLocationException;
//import sun.text.normalizer.ReplaceableString;

/**
 *
 * @author Manoel
 */
public class Editor extends javax.swing.JFrame implements DocumentListener {

    /**
     * Creates new form Editor
     */
    final static Color HILIT_COLOR = Color.LIGHT_GRAY;
    final static Color ERROR_COLOR = Color.PINK;
    final static Color RIGHT_COLOR = Color.green;
    final Color entryBg;
    final Highlighter hilit;
    final Highlighter.HighlightPainter painter;

    public Editor() {
        initComponents();

        hilit = new DefaultHighlighter();
        painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
        jTextArea1.setHighlighter(hilit);

        entryBg = texto1.getBackground();
        texto1.getDocument().addDocumentListener(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        texto1 = new javax.swing.JTextField();
        texto2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButtonTVC = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        MArquivo = new javax.swing.JMenu();
        MSalvarArquivo = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        MSair = new javax.swing.JMenuItem();
        MEditar = new javax.swing.JMenu();
        MFerramentas = new javax.swing.JMenu();
        MContarPalavras = new javax.swing.JMenuItem();
        MContarFrases = new javax.swing.JMenuItem();
        jMenuItemPRepetidas = new javax.swing.JMenuItem();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(80);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setWrapStyleWord(true);
        jScrollPane2.setViewportView(jTextArea1);

        texto1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                texto1ActionPerformed(evt);
            }
        });

        texto2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                texto2ActionPerformed(evt);
            }
        });

        jButton1.setText("substituir");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton1MousePressed(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Procurar:");

        jLabel2.setText("Substituir:");

        jButtonTVC.setText("Trocar valor dos campos");
        jButtonTVC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButtonTVCMousePressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(texto1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(texto2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addComponent(jButtonTVC)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jLabel1)
                    .addComponent(texto1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(texto2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonTVC)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane1.setViewportView(jTextArea2);

        MArquivo.setText("Arquivo");

        MSalvarArquivo.setText("Salvar arquivo");
        MSalvarArquivo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                MSalvarArquivoMousePressed(evt);
            }
        });
        MArquivo.add(MSalvarArquivo);
        MArquivo.add(jSeparator1);

        MSair.setText("Sair");
        MSair.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                MSairMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                MSairMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                MSairMousePressed(evt);
            }
        });
        MArquivo.add(MSair);

        jMenuBar1.add(MArquivo);

        MEditar.setText("Editar");
        jMenuBar1.add(MEditar);

        MFerramentas.setText("Ferramentas");

        MContarPalavras.setText("Contar Palavras");
        MContarPalavras.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                MContarPalavrasMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                MContarPalavrasMousePressed(evt);
            }
        });
        MFerramentas.add(MContarPalavras);

        MContarFrases.setText("Contar Frases");
        MContarFrases.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                MContarFrasesMousePressed(evt);
            }
        });
        MFerramentas.add(MContarFrases);

        jMenuItemPRepetidas.setText("Palavras repetidas");
        jMenuItemPRepetidas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuItemPRepetidasMousePressed(evt);
            }
        });
        MFerramentas.add(jMenuItemPRepetidas);

        jMenuBar1.add(MFerramentas);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void MSairMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_MSairMouseClicked
        System.exit(0);
    }//GEN-LAST:event_MSairMouseClicked

    private void MSairMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_MSairMouseExited
        
    }//GEN-LAST:event_MSairMouseExited

    private void MContarPalavrasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_MContarPalavrasMouseClicked
    }//GEN-LAST:event_MContarPalavrasMouseClicked

    private void MContarPalavrasMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_MContarPalavrasMousePressed
        Core contador = new Core();
        JOptionPane.showMessageDialog(null, "Numero de palavras no texto: \n"
                + contador.contaPalavras(jTextArea1.getText()),
                "Contador de Palavras",JOptionPane.PLAIN_MESSAGE);
    }//GEN-LAST:event_MContarPalavrasMousePressed

    private void MSairMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_MSairMousePressed
        System.exit(0);
    }//GEN-LAST:event_MSairMousePressed

    private void MContarFrasesMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_MContarFrasesMousePressed
        Core contador = new Core();
        JOptionPane.showMessageDialog(null, "Numero de frases no texto: \n"
                + contador.contarFrases(jTextArea1.getText())
                + contador.frase(contador.contarFrases(jTextArea1.getText())),
                "Contador de Frases", JOptionPane.PLAIN_MESSAGE);
    }//GEN-LAST:event_MContarFrasesMousePressed

    private void MSalvarArquivoMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_MSalvarArquivoMousePressed
        Salvar s = new Salvar(this);
        s.salvarArquivo(jTextArea1.getText());
    }//GEN-LAST:event_MSalvarArquivoMousePressed

    private void texto1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_texto1ActionPerformed
        
    }//GEN-LAST:event_texto1ActionPerformed

    private void jMenuItemPRepetidasMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItemPRepetidasMousePressed
        Core c = new Core();
        JOptionPane.showMessageDialog(null,"Palavras e sua quantidade: \n"+
                Core.ContadorDeRepeticoes(jTextArea1.getText()),
                "Palavras Repetidas",JOptionPane.PLAIN_MESSAGE);
    }//GEN-LAST:event_jMenuItemPRepetidasMousePressed

    private void texto2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_texto2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_texto2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MousePressed
            substituir();       
    }//GEN-LAST:event_jButton1MousePressed

    private void jButtonTVCMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonTVCMousePressed
        trocar_valor_campos();
    }//GEN-LAST:event_jButtonTVCMousePressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    UIManager.put("swing.boldMetal", Boolean.FALSE);
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Editor().setVisible(true);


            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu MArquivo;
    private javax.swing.JMenuItem MContarFrases;
    private javax.swing.JMenuItem MContarPalavras;
    private javax.swing.JMenu MEditar;
    private javax.swing.JMenu MFerramentas;
    private javax.swing.JMenuItem MSair;
    private javax.swing.JMenuItem MSalvarArquivo;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonTVC;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItemPRepetidas;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextField texto1;
    private javax.swing.JTextField texto2;
    // End of variables declaration//GEN-END:variables

    public void search() {
        hilit.removeAllHighlights();

        String s = texto1.getText();
        if (s.length() <= 0) {
            return;
        }

        String content = jTextArea1.getText();
        int index = content.indexOf(s, 0);
        if (index >= 0) {   // match found
            try {
                int end = index + s.length();
                hilit.addHighlight(index, end, painter);
                jTextArea1.setCaretPosition(end);
                texto1.setBackground(entryBg);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        } else {
            texto1.setBackground(ERROR_COLOR);
        }


    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        search();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        search();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        search();
    }
    
    
    public void substituir() {
        String texto = jTextArea1.getText().replace(texto1.getText(), texto2.getText());
        jTextArea1.setText(texto);
    }
    
    public void trocar_valor_campos(){
        String aux = texto1.getText();
        texto1.setText(texto2.getText());
        texto2.setText(aux);
    }
}
